//
//  MHAskTableViewCell.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/28.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHAskTableViewCell.h"
//#import "mhas"
#import "MHAskItemViewModel.h"
#import "MHPhotoManager.h"
#import "UIView+JKRViewController.h"

@interface MHAskTableViewCell()
@property(nonatomic,strong) MHAskItemViewModel *viewModel;
@end

@implementation MHAskTableViewCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)bindViewModel:(MHAskItemViewModel *)viewModel
{
    self.viewModel = viewModel;
    self.nickNameLable.text = viewModel.model.nickName;
//    self.descLabel.text = viewModel.model.qaInfo;
    @weakify(self);
    [RACObserve(self.viewModel.model, qaInfo)
     subscribeNext:^(id x) {
         @strongify(self)
         NSString *descString = (NSString *) x;
         if (descString.length <= 0) {
             x = @"Ta很懒，没有介绍自己！";
             self.descLabel.textColor = MHMetionTextColor;
         }else
         {
             self.descLabel.textColor = MHMainTextColor;
         }
         self.descLabel.text = x;
     }];

    [self.avaterImageView yy_setImageWithURL:[NSURL URLWithString:viewModel.model.fullUserFace] placeholder:MHDefaultAvatar(MHDefaultAvatarTypeDefualt)];
    [self.answerBtn setTitle:[NSString stringWithFormat:@"TA的回答(%d)",viewModel.model.answerCount] forState:0];
    if (viewModel.isAll) {
        [self.askBtn setTitle:@"向所有人提问" forState:0];
        [RACObserve(self.viewModel.model, askCount)
         subscribeNext:^(NSNumber *x) {
             @strongify(self)
           NSString *askString =  [NSString stringWithFormat:@"我的提问(%d)",[x intValue]];
           [self.answerBtn setTitle:askString forState:0];
         }];
    }else
    {
        [self.askBtn setTitle:@"向TA提问" forState:0];
    }
   
    
    if([MHHTTPService sharedInstance].currentUser.userId == self.viewModel.model.userId)
    {
        self.editBtn.hidden = false;
    }else
    {
        self.editBtn.hidden = YES;
    }
    if (viewModel.isAll) {
        self.editBtn.hidden = viewModel.isAll;
    }
    

   
    
}
- (IBAction)anserAction:(id)sender {
   
    [self.viewModel.answerCommand execute:nil];
}
- (IBAction)askAction:(id)sender {
    if (self.viewModel.model.currentMemberIsOwner) {
        [MBProgressHUD mh_showTips:@"不能向自己提问"];
        return;
    }
    [MHPhotoManager fetchPhotosFromAlbum:self.jkr_viewController maxImagesCount:1 allowCrop:NO selectedAssets:nil completion:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto, NSArray<NSDictionary *> *infos) {
        NSMutableArray *photesArray =  [MHPhotoManager selectedAlbumPhotosIncludingGifWithPHAssets:assets photos:photos];
        self.viewModel.selectAsserts = assets;
         [self.viewModel.askCommand execute:photesArray];
    } cancel:^{
    }];
   
}
- (IBAction)editInfoAction:(id)sender {
    
    if ([MHHTTPService sharedInstance].currentUser.userId == self.viewModel.model.userId ) {
         [self.viewModel.editInfoCommand execute:self.viewModel.model];
    }else
    {
        [MBProgressHUD mh_showTips:@"只能修改自己的资料"];
    }
   
}

@end
